﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cms.Web.Framework.Controls;
using Cms.Application.Admin.Controllers;
using Cms.Application.Admin.Views;
using Cms.Application;
using Cms.Domain;
//using Brettle.Web.NeatUpload;
using Cms.Web.Framework.Infrastructure.Authentication;

namespace Cms.Web.UI.Admin.TinyMCEIntegration
{

    public partial class UploadPopup : System.Web.UI.Page, IUploadView
    {

        private UploadController _controller;

        
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Response.HeaderEncoding = System.Text.Encoding.UTF8;
            baseTag.Attributes.Add("href", iLib.Web.UI.Url.FullPath);
            Page.Header.Controls.Add(new iLib.Web.UI.iLibRegistrator());
            _controller = new UploadController(this);
            this.Title = Translator.GetText(this.ContextTranslationFile, "Heading", this.Localization);
            if (!Page.IsPostBack)
            {
                //_controller.Initialize();
                form1.Action = iLib.Web.UI.Url.FoldersAndPage + iLib.Web.UI.Url.Querystring;
                hlCancel.NavigateUrl = "Admin/TinyMCEIntegration/MediaPopup.aspx";
            }
            string script = "function displayOverwriteConfirmation(existingFiles) { " +
                        "document.getElementById('lblExistingFiles').innerHTML = existingFiles;" +
                        "cms.Dialogs.Confirm(validationCallback, '" + Translator.GetText(this.ContextTranslationFile, "OverwriteHeader", this.Localization) + "', null, '" + Translator.GetText(this.ContextTranslationFile, "OverwriteOK", this.Localization) + "', '" + Translator.GetText(this.ContextTranslationFile, "OverwriteCancel", this.Localization) + "', true, null, 'overwriteWarning');" +
                    "}";
            iLib.Web.UI.ClientScriptProxy.Current.RegisterClientScriptBlock(this, this.GetType(), "validate", script, true);
            ifrCheckFiles.Attributes.Add("src", iLib.Web.UI.Url.VirtualPath + "Admin/UploadCheck.aspx");
        }


        protected void lbCheckUpload_Click(object sender, EventArgs e)
        {
            // This method will only fire if the Upload button is clicked when the selected files list is empty
            Response.Redirect("MediaPopup.aspx");
        }


        protected void lbStartUpload_Click(object sender, EventArgs e)
        {
            foreach (UploadedFile file in multiFileId.Files)
            {
                //_controller.SaveUploadedFile();
            }
            Response.Redirect("MediaPopup.aspx");
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/Upload.xml"; }
        }


        public string UserPhysicalTempDirectory
        {
            get { return "Admin\\Resources\\Temp\\" + this.AdministratorID + "\\"; }
        }


        public string UserTempDirectory
        {
            get { return iLib.Web.UI.Url.FullPath + "Admin/Resources/Temp/" + this.AdministratorID + "/"; }
        }


        public virtual int AdministratorID
        {
            get
            {
                AuthenticatedUser user = Conversion.ToAuthenticatedUser(User.Identity.Name);
                return user.AdministratorID;
            }
        }


        public string Localization
        {
            get
            {
                HttpCookie cookie = Request.Cookies["FairportAdminLocalization"];
                if (cookie != null)
                    return cookie.Value;
                else
                    return "en-US";
            }
        }

        public void SetFolderName(string name)
        {
            litFolder.Text = name;
        }

    }

}
